import {createStore} from 'redux'
import todoApp from '../Reducer/index'
import {
    addTodo,
    toggleTodo,
    setVisibilityFilter, VisibilityFilters
} from "../Action/ActionTypes";


let store = createStore(todoApp);
// let store = createStore(todoApp, window.STATE_FROM_SERVER) ;// 的第二个参数是可选的, 用于设置 state 初始状态。
// 这对开发同构应用时非常有用，服务器端 redux 应用的 state 结构可以与客户端保持一致, 那么客户端可以将从网络接收到的服务端 state 直接用于本地数据初始化。

export default store;


//测试
console.log('getState:',store.getState())

const unsubscribe = store.subscribe(()=>console.log(store.getState()));//监听器里调用getState获取当前state树

store.dispatch(addTodo('first todo item'))
store.dispatch(addTodo('Learn about reducers'))
store.dispatch(addTodo('Learn about store'))
store.dispatch(toggleTodo(0))
store.dispatch(setVisibilityFilter(VisibilityFilters.SHOW_ACTIVE))


unsubscribe();//停止监听state 更新
store.dispatch(addTodo('Stop listen Learn about store'));//不会触发